<template>
  <!-- 添加或修改签字信息对话框 -->
  <el-dialog :visible.sync="open" width="600px">
    <div slot="title" class="dialog-title">
      <i :class="titleIcon"></i>
      <span class="title-text">{{ title }}</span>
      <div class="button-right">
        <span class="title-close" @click="cancel"></span>
      </div>
    </div>

    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item v-show="showIncept" label="接  收  人" prop="recipientName">
        <!-- <el-input v-model="form.recipient" placeholder="请输入接收人姓名" /> -->

      </el-form-item>
      <el-form-item :label="dialogType=='sign' ? '签发意见' : '提交意见'" prop="comment">
        <el-input
          v-model="form.comment"
          type="textarea"
          placeholder="请选择或输入检阅意见"
          rows="4"
          resize="none"
        />
        <el-radio-group v-model="selectValue" @change="changeRadioVal">
          <el-radio label="同意">同意</el-radio>
          <el-radio v-show="dialogType!='sign'" label="请审核">请审核</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" v-preventRepeat @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { publish, submitTicket } from './SignList'
import store from '@/store/index'
import { getFunNo } from '@/utils/party'
import {NAME} from '@/utils/config'
export default {
  name: 'AddSign',
  components: {
  },
  props: {
    dialogFormData: {
      Type: Object,
      default: () => {
        return {}
      },
    },
    dialogType: {
      Type: String,
      default: () => {
        return null
      },
    },
    taskreportList:{
      default:true,
      type:Boolean
    }
  },
  data() {
    return {
      // radio选择
      selectValue: '同意',
      funNo: getFunNo(this.$route),
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: '提交',
      titleIcon: 'el-icon-edit-outline',
      // 是否显示接收人
      showIncept: false,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        // 发送类型
        recipientType: 'p',
        // 接收人
        recipient: '',
        // 接收人姓名
        recipientName: '',
        // 签发意见
        comment: null,
        // 功能号
        functionNo: null,
        // 单据号
        businessNo: null,
        // 是否为签发通知
        notifyOthers: false,
      },
      misTreeType: 103,
      // 表单校验
      rules: {
        comment: [
          { required: true, message: '意见必须选择或输入', trigger: 'blur' },
        ],
        recipient: [
          { required: true, message: '请选择接收人', trigger: 'blur' },
        ],
      },
      // 单据提交类型，sign：签字，submit：提交
      signType: {
        sign: 'sign',
        submit: 'submit',
      },
    }
  },
  created() {
    this.funNo = getFunNo(this.$route)
    if(this.$route.fullPath === '/gzw/taskreportList') {
      this.funNo =getFunNo(this.$router.resolve({ name: NAME }).route)
    }
  },
  methods: {
    handleOpenDialog() {
      // 当前页面展示出来！
      this.open = true
      this.reset()
      //初始化
      this.initForm()
      this.$nextTick(() => {
      })
    },
    handleCloseDialog() {
      this.open = false
    },
    updateTreeTransferVal(value) {
      let valArr = value.split('|')
      if (valArr.length > 1) {
        this.form.recipientName = valArr[0]
        this.form.recipient = valArr[1]
      }
    },
    // 初始化默认值
    initForm() {
      // 处理图标问题
      if (this.dialogType == this.signType.sign) {
        this.title = '单据签发'
        this.titleIcon = 'el-icon-edit-outline'
        this.showIncept = false
      } else {
        this.title = '单据提交'
        this.titleIcon = 'el-icon-upload2'
        this.showIncept = true
      }
      //签字意见
      this.selectValue = '同意'
      this.form.comment = this.selectValue
      this.form.businessNo = this.dialogFormData.selectedCt.ct_id
      this.form.functionNo = this.dialogFormData.selectedCt.ct_fun_no
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        // 发送类型
        recipientType: 'p',
        // 接收人
        recipient: '',
        // 接收人姓名
        recipientName: '',
        // 签发意见
        comment: '',
        // 功能号
        functionNo: null,
        // 单据号
        businessNo: null,
        // 是否为签发通知
        notifyOthers: false,
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.subFunNo = this.dialogFormData.selectedCt.ct_fun_no
      // 先判断是否已经修改、保存过单据的数据
      this.singleStore = store.getters.singleStore['main' + this.funNo]
      this.subSingleStore =
        this.singleStore.subSingleStore['sub' + this.subFunNo]
      if (this.subSingleStore && this.subSingleStore.changeForm) {
        this.$message({
          type: 'info',
          message: '请先保存单据中的信息',
        })
        this.open = false
        return
      }
      if (this.form.comment === '') {
        this.$message.warning('请输入意见')
        return
      }
      // 签发
      if (this.dialogType == this.signType.sign) {
        publish(this.form).then((response) => {
          if (response && response.code == 200) {
            this.msgSuccess(response.msg)
            // 设置ifEnd
            this.dialogFormData.selectedCt.ct_end_name = '已签发'
            this.$emit('refreshAction')
          } else {
            this.mesError(response.msg)
          }
          this.open = false
        })
      } else if (this.dialogType == this.signType.submit) {
        // 单据提交
        submitTicket(this.form).then((response) => {
          if (response && response.code == 200) {
            this.msgSuccess(response.msg)
            store.dispatch('setChangeListTrue', this.funNo)
            this.$emit('refreshAction')
            this.reset()
          } else {
            this.msgError(response.msg)
          }
          this.open = false
        })
      }
    },
    //radio 更新
    changeRadioVal(value) {
      this.form.comment = value
    },
  },
}
</script>
<style scoped>
.el-dialog {
  background: rgb(250, 249, 254);
}

.dialog-title {
  text-align: left;
  font-size: 18px;
  font-weight: 700;
  overflow: hidden;
}

.dialog-title i {
  color: #45a4f9;
  /* color: #0B2278; */
  font-size: 18px;
  line-height: 38px;
}

.dialog-footer {
  text-align: right;
  font-size: 14px;
  font-weight: 700;
}
</style>
